package MyRedis

import (
	"context"
	"time"
)

/**
StringOperation 字符串操作
*/
type StringOperation struct {
	ctx context.Context
}

func NewStringOperation() *StringOperation {
	return &StringOperation{ctx: context.Background()}
}

/**
Get 获取缓存
*/
func (o *StringOperation) Get(str string) *StringResult {
	return NewStringResult(MRedis().Get(o.ctx, str).Result())
}

/**
Set 设置缓存
*/
func (o *StringOperation) Set(key string, value interface{}, attrs ...*OperationAttr) *InterfaceResult {
	exp := OperationAttrs(attrs).Find(ATTR_EXPIRE).UnwrapDefault(time.Second * 0).(time.Duration)
	nx := OperationAttrs(attrs).Find(ATTR_NX).UnwrapDefault(nil)
	if nx != nil {
		return NewInterfaceResult(MRedis().SetNX(o.ctx, key, value, exp).Result())
	}
	xx := OperationAttrs(attrs).Find(ATTR_XX).UnwrapDefault(nil)
	if xx != nil {
		return NewInterfaceResult(MRedis().SetXX(o.ctx, key, value, exp).Result())
	}
	return NewInterfaceResult(MRedis().Set(o.ctx, key, value, exp).Result())
}

/**
MGet
*/
func (o *StringOperation) MGet(key ...string) *SliceResult {
	return NewSliceResult(MRedis().MGet(o.ctx, key...).Result())
}
